package net.ib.mtalk.network.task;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Common.SuggestionId;
import net.ib.mntalk.protocol.Common.SuggestionIdSliceRange;
import net.ib.mntalk.protocol.Common.SuggestionInfo;
import net.ib.mntalk.protocol.Common.UUIDMsg;
import net.ib.mntalk.protocol.Session.AddBuddyReq;
import net.ib.mntalk.protocol.Session.GetBuddyChangesRes;
import net.ib.mntalk.protocol.Session.GetSuggestionsReq;
import net.ib.mntalk.protocol.Session.GetSuggestionsRes;
import net.ib.mntalk.protocol.Session.SearchBuddyReq;
import net.ib.mntalk.protocol.Session.SearchBuddyRes;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.constant.MTalkConstant;
import net.ib.mtalk.database.helper.SuggestionDatabaseHelper;
import net.ib.mtalk.global.GlobalPreferences;
import net.ib.mtalk.network.MethodId.Session;
import net.ib.mtalk.network.TcpEnvelope;
import net.ib.mtalk.ui.buddy.suggestion.BuddySuggestionListActivity;
import net.ib.mtalk.ui.buddy.talk.BuddyTalkListActivity;
import net.ib.mtalk.util.GpbSupport;
import net.ib.mtalk.util.MTalkLogWriter;
import net.ib.mtalk.util.NotifyingAsyncQueryHandler;

public class SearchBuddyTask extends AsyncNetTask {
	
	private SearchBuddyReq mSearchBuddyReq;

	public SearchBuddyTask(Context _context, Handler _uiHandler, SearchBuddyReq searchBuddyReq) {
		super(_context, _uiHandler);
		this.mSearchBuddyReq = searchBuddyReq;
	}

	@Override
	protected TcpEnvelope onPreExecute() {
		TcpEnvelope rev = new TcpEnvelope.Builder()
			.setType(TcpEnvelope.REQUEST)
			.setMessage(mSearchBuddyReq)
			.setMethodId(Session.Contact.SearchBuddy)
			.build();
		return rev;
	}

	@Override
	protected void onPostExecute(int resultCode, Object sentObject, Object receivedObject) {
		MTalkLogWriter.d("SearchBuddyTask.onPostExecute() resultCode : " + resultCode);
		if (receivedObject == null) {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNKNOWN_ERROR));
			return;
		}
		
		TcpEnvelope resultEnvelope = (TcpEnvelope) receivedObject;
		SearchBuddyRes searchBuddyRes = (SearchBuddyRes) resultEnvelope.getMessage();
		
		MTalkLogWriter.d("SearchBuddyTask ResponseCode : " + searchBuddyRes.getResponseCode());
		
		if(searchBuddyRes.getResponseCode() == Common.SUCCESS){
			AddBuddyReq addBuddyReq = new AddBuddyReq();
			addBuddyReq.setBuddy(searchBuddyRes.getBuddy());
			InsertBuddyTask task = new InsertBuddyTask(mContext, mUiHandler, addBuddyReq);
			task.execute();
//			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_SEARCHBUDDY_SUCCESS));
		} else if(searchBuddyRes.getResponseCode() == Common.NOT_EXIST_OBJECT){
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_SEARCHBUDDY_NOT_EXIST));
		} else {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_SEARCHBUDDY_FAIL));
		}

	}

}
